package com.tensquare.qa.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.qa.pojo.Reply;
import com.tensquare.qa.service.ReplyService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reply")
@CrossOrigin
public class ReplyController {
   @Autowired
   private  ReplyService replyService;
    @PostMapping
    public Result save(@RequestBody Reply reply){
        replyService.save(reply);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/reply 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Reply>replys=replyService.findAll();
        return new Result(true,StatusCode.OK,"ok",replys);
    }
    // GET/reply/{replyId}根据ID查询
    @GetMapping("/{replyId}")
    public Result findByReplyId(@PathVariable String replyId) {
        Reply reply=replyService.findById(replyId);
        return new Result(true,StatusCode.OK,"ok",reply);
    }
    // PUT/reply/{replyId}修改标签
    @PutMapping("{replyId}")
    public Result findByReplyId(@PathVariable String replyId,@RequestBody Reply reply) {
        reply.setId(replyId);
        replyService.updateByReplyId(reply);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/reply/{replyId}根据ID删除
    @DeleteMapping("{replyId}")
    public  Result deleteByReplyId(@PathVariable String replyId){
        replyService.deleteByReplyId(replyId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /reply/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<Reply> pageData=replyService.findByPage(map,page,size);
        PageResult<Reply> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /reply/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Reply> list=replyService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }

   // GET/reply/problem/{problemId}根据问题ID查询回答列表
    @GetMapping("/problem/{problemId}")
    public Result findByProblemId(@PathVariable String problemId) {
        List<Reply> replies=replyService.findByProblemId(problemId);
        return new Result(true,StatusCode.OK,"ok",replies);
    }


        //POST/reply/save回答问题
    @PostMapping("/save")
    public Result say(@RequestBody Reply reply){
        replyService.save(reply);
        return new Result(true, StatusCode.OK,"ok");
    }

    //GET/reply/list/{page}/{size}我的回答列表
  /*  @GetMapping("/list/{page}/{size}")
    public  Result findMyList(){

    }*/

}
